//=============================================================================
// System  : MatchKit
// File    : Utils.cs
// Author  : Fabio Bussu  (matchkit@fabiobussu.it)
// Updated : 04/16/2013
// Note    : Copyright 2013, Fabio Bussu, All rights reserved
//
// This code is published under the Microsoft Public License (Ms-PL). A copy
// of the license should be distributed with the code.  It can also be found
// at the project website: http://matchkit.codeplex.com. This notice, the
// author's name, and all copyright notices must remain intact in all
// applications, documentation, and source files.
//
// Version     Date     Who  Comments
// ============================================================================
// 1.0.0.0  04/16/2013   FB  First draft
//=============================================================================
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace MatchKit
{
	static class Utils
	{
		public static bool AnyNull<T>(T[] array)
		{
			if (array != null)
			{
				for (int i = 0; i < array.Length; i++)
				{
					if (array[i] == null)
						return true;
				}
			}
			return false;
		}

		public static string Join(string separator, params char[] values)
		{
			if (values == null || values.Length == 0)
				return null;
			var sb = new StringBuilder(values.Length * 2);
			for (int i = 0; i < values.Length; i++)
			{
				if (i != 0)
					sb.Append(separator);
				sb.Append(values[i]);
			}
			return sb.ToString();
		}

		public static string[] GetNames(
			#if EM
			this 
			#endif
			Type enumType)
		{
			if (!enumType.IsEnum)
				throw new ArgumentException("enumType must be an Enum", "enumType");

			var nameList = new List<string>();
			var fiArray = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);

			foreach (var fi in fiArray)
				nameList.Add(fi.Name);

			return nameList.ToArray();
		}

		public static ConstructorInfo GetDefaultConstructor(Type type, bool alsoPrivate = true)
		{
			var bflags = BindingFlags.Instance | BindingFlags.Public;
			if (alsoPrivate)
				bflags |= BindingFlags.NonPublic;
			foreach (var ctor in type.GetConstructors(bflags))
			{
				if (ctor.GetParameters().Length == 0)
					return ctor;
			}
			return null;
		}

		public static bool HasDefaultConstructor(Type type, bool alsoPrivate = true)
		{
			var bflags = BindingFlags.Instance | BindingFlags.Public;
			if (alsoPrivate)
				bflags |= BindingFlags.NonPublic;
			foreach (var ctor in type.GetConstructors(bflags))
			{
				if (ctor.GetParameters().Length == 0)
					return true;
			}
			return false;
		}
	}
}
